package com.masterclient.app.businessmodule.minemodule.message;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.bean.personal.OtherMsgBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.message.adapter.OtherMsgAdapter;
import com.masterclient.app.businessmodule.minemodule.message.contract.OtherMsgContract;
import com.masterclient.app.businessmodule.minemodule.message.contract.OtherMsgPresenter;
import com.masterclient.app.utils.CommonUtil;

import butterknife.BindView;

/**
 * @auther hjian
 * @date 2020/12/8.
 */
public class OtherMsgActivity extends FCBusinessActivity<OtherMsgPresenter> implements OtherMsgContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private OtherMsgAdapter otherMsgAdapter;
    private View emptyView;
    private int page;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_other_msg;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("其他消息");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setPadding(0, 0,0,ScreenUtils.dp2px(16));
        emptyView = View.inflate(this,R.layout.empty_msg,null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        otherMsgAdapter = new OtherMsgAdapter();
        otherMsgAdapter.bindToRecyclerView(recyclerView);
        otherMsgAdapter.setEmptyView(emptyView);
        emptyView.setVisibility(View.GONE);

        initListener();
        refreshData(1,false);
    }

    private void refreshData(int page,boolean isRefresh) {
        this.page = page;
        mPresenter.getMsgList(page,isRefresh);
    }

    @Override
    public void showData(OtherMsgBean bean) {
        if (baseRefreshLayout().isRefreshing()){
            baseRefreshLayout().setRefreshing(false);
        }
        otherMsgAdapter.loadMoreComplete();

        if (page == 1){
            otherMsgAdapter.setNewData(bean.getMessage_list());
            emptyView.setVisibility(bean.getMessage_list() == null
                    || bean.getMessage_list().size() == 0 ?View.VISIBLE:View.GONE);
        }else {
            otherMsgAdapter.addData(bean.getMessage_list());
        }


        if(bean.getMessage_list() == null || bean.getMessage_list().size()<1 || !bean.isHas_more()){
            otherMsgAdapter.setEnableLoadMore(false);
        }else {
            ++page;
        }

    }

    @Override
    public void showFailure(String msg) {

        ToastUtil.showToast(msg);
        if (baseRefreshLayout().isRefreshing()){
            baseRefreshLayout().setRefreshing(false);
        }
        otherMsgAdapter.loadMoreComplete();

    }

    private void initListener(){
        baseRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData(1,true);
            }
        });

        otherMsgAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                    refreshData(page,false);
            }
        });

        otherMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CommonUtil.goAnyWhere(OtherMsgActivity.this, otherMsgAdapter.getData().get(position).getTarget()
                        ,otherMsgAdapter.getData().get(position).getTarget_id(),"","","");
            }
        });
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        refreshData(1,false);
    }
}
